package mju.cxf.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import mju.cxf.constant.MessageConstant;
import mju.cxf.dto.CommentsDTO;
import mju.cxf.entity.Comments;
import mju.cxf.result.Result;
import mju.cxf.service.CommentsService;
import mju.cxf.service.impl.PhotoServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 商品出售相关接口
 */

@RestController
@Api(tags = "出售接口列表")
@RequestMapping("/admin/api/comments")
public class CommentsController {

@Autowired
private CommentsService commentsService;
@Autowired
private PhotoServiceImpl photoService;

    /**
     * 修改出售商品信息
     *
     * @return
     */
    @PutMapping("/update")
    @ApiOperation(value = "审核评论信息")
    public Result<String> updateComments(@RequestBody Comments comments) {
        if (commentsService.updateComments(comments)) {
            if (comments.getStatus()==1) {
                photoService.saveImages(comments.getPicture());
            }
            Result.success(MessageConstant.SUCCESS_MESSAGE);
        }
        return Result.error(MessageConstant.FAILURE_MESSAGE);
    }


//    /**
//     * 分页查询出售信息
//     * @return 分页查询结果
//     */
//    @GetMapping("/pages")
//    public Page<Sales> getUsersByUsername(@RequestParam(value = "name",required = false) String name,
//                                          @RequestParam(value = "current",defaultValue = "1") int current,
//                                          @RequestParam(value = "size",defaultValue = "10") int size) {
//        return salesService.selectPageByName(name, current, size);
//    }

    /**
     * 删除商品信息
     *
     * @param id 删除id
     * @return 删除结果
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除出售信息", notes = "管理员通过此接口删除出售信息")
    public Result<String> deleteComments(@PathVariable Integer id) {
        return commentsService.deleteComments(id) ?
                Result.success(MessageConstant.SUCCESS_MESSAGE) :
                Result.error(MessageConstant.FAILURE_MESSAGE);
    }

    /**
     * 分页查询出售信息
     *
     * @param name    可选，根据用户名进行模糊查询
     * @param status   可选，根据出售状态进行查询
     * @param current 当前页码
     * @param size    每页显示的记录数
     * @return 分页查询结果
     */
    @GetMapping("/pages")
    @ApiOperation(value = "分页查询出售信息", notes = "根据用户名和出售状态分页查询")
    public Page<CommentsDTO> getComments(
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "status", required = false) Integer status, // 添加状态参数
            @RequestParam(value = "current", defaultValue = "1") int current,
            @RequestParam(value = "size", defaultValue = "10") int size) {
        return commentsService.selectComments(name, status, current, size);
    }
}
